from __future__ import absolute_import

from file_parser import *
from system_util import *
from make_file_base import *

# ------------------------------------------------------------------------------------------------------#

def make_class_name(file_name):
  flag = True
  class_name = ''
  for i in range(len(file_name)):
    if file_name[i] == '_':
      flag = True
    elif flag:
      flag = False
      class_name += file_name[i].upper()
    else:
      class_name += file_name[i]

  return class_name.replace('Nweb', 'NWeb')

# ------------------------------------------------------------------------------------------------------#

def make_include_file(file_name):
  content = get_copyright()

  content += \
"""
#ifndef $GUARD$
#define $GUARD$
#pragma once
"""
  content += '\n'

  content += '#include "include/base/ark_web_base_ref_counted.h"\n'
  content += '\n'

  class_name = make_class_name(file_name)
  content += 'namespace OHOS::ArkWeb {\n\n' + \
             '/*--ark web(source=library)--*/\n' + \
             'class ' + class_name + ' : public virtual ArkWebBaseRefCounted {\n' + \
             'public:\n' + \
             '  /*--ark web()--*/\n' + \
             '};\n\n' + \
             '} // namespace OHOS::ArkWeb\n'

  content += \
"""
#endif // $GUARD$
"""

  content = content.replace('$GUARD$', file_name.upper() + '_H_')
  write_file('../include/' + dir_name + '/' + file_name + '.h', content)

# ------------------------------------------------------------------------------------------------------#

def make_wrapper_file(file_name, base_name):
  content = get_copyright()

  content += \
"""
#ifndef $GUARD$
#define $GUARD$
#pragma once
"""
  content += '\n'

  content += '#include "ctocpp/' + dir_name + '/' + file_name + '_ctocpp.h"\n'
  content += '\n'
  
  class_name = make_class_name(file_name)
  wrapper_name = class_name + 'Wrapper'
  content += 'namespace OHOS::ArkWeb {\n\n' + \
             'class ' + wrapper_name + ' : public OHOS::NWeb::' + base_name + ' {\n' + \
             'public:\n' + \
             '  ' + wrapper_name + '(' + class_name + 'CToCpp ' + file_name + ');\n\n' + \
             'private:\n' + \
             '  ' + class_name + 'CToCpp ' + file_name + '_;\n' + \
             '};\n\n' + \
             '} // namespace OHOS::ArkWeb\n'

  content += \
"""
#endif // $GUARD$
"""

  content = content.replace('$GUARD$', file_name.upper() + '_WRAPPER_H_')
  write_file('../wrapper/' + dir_name + '/' + file_name + '_wrapper.h', content)

  content = get_copyright()
  content += '\n'

  content += '#include "wrapper/' + dir_name + '/' + file_name + '_wrapper.h"\n'
  content += '\n'

  content += 'namespace OHOS::ArkWeb {\n'
  content += '\n'

  content += wrapper_name + '::' + wrapper_name + '(' + class_name + 'CToCpp ' + file_name + ')\n' + \
             '    : ' + file_name + '_(' + file_name + ') {}\n\n'

  content += '} // namespace OHOS::ArkWeb\n'

  write_file('../wrapper/' + dir_name + '/' + file_name + '_wrapper.cc', content)

# ------------------------------------------------------------------------------------------------------#

dir_name = 'ohos_nweb'

# cannot be loaded as a module
if __name__ != "__main__":
  sys.stderr.write('This file cannot be loaded as a module!')
  sys.exit()

with open('file.txt', 'r') as f:
  for line in f:
    lst = line.strip().split(' ')
    make_include_file(lst[0].lower())
    make_wrapper_file(lst[0].lower(), lst[1])

